package com.commons.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.commons.service.SiteConstService;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.ryun.bean.UserBean;

@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ModelDriven<Object>, Preparable {

	public com.commons.service.SiteConstService siteConstService = null;

	protected HttpServletRequest request;
	protected HttpServletResponse response;

	public UserBean userBean = new UserBean();;
	
	public String contextPath = "";
	
	public User user = null;
	public Date date = new Date();
	
	@SuppressWarnings("rawtypes")
	public Map params;

	private void setHeader() {
		response.setContentType("text/html; charset=utf-8");
		
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("Pragma","no-cache");
		response.setHeader("Expires","0");
		
		if(request.getContextPath() != null && !"".equals(request.getContextPath())){
			contextPath = request.getContextPath();
		}
	}

	private void makeLoginSession() throws Exception {
		UserService userService = UserServiceFactory.getUserService();
		user = userService.getCurrentUser();
		
		String thisURL = request.getRequestURI();
		if(params.get("type") != null && params.get("type") != ""){
			thisURL += "?type=" + params.get("type");
		}
		
		if(user != null){
			userBean.setEmail(user.getEmail());
			userBean.setNickname(user.getNickname());
			userBean.setUserId(user.getUserId());
			userBean.setLogin(userService.isUserLoggedIn());
			userBean.setAdmin(userService.isUserAdmin());
			userBean.setUrlAccount(userService.createLogoutURL(thisURL));
		}else{
			userBean.setLogin(userService.isUserLoggedIn());
			userBean.setUrlAccount(userService.createLoginURL(thisURL));
		}
		
		thisURL = null;
		userService = null;
		//user = null;
	}

	public static void main(String[] args) {
		
		Date tmpDate = new Date();
		Date date = new Date();
	    TimeZone tz;
	    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z Z)");

	    tz = TimeZone.getTimeZone("Asia/Seoul");
	    df.setTimeZone(tz);
	    
	    try {
			date = df.parse(df.format(tmpDate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    System.err.println("#@#!#!#@!#! " + date);
		
	}
	public void makeKoDate() throws Exception{
	    TimeZone tz;
	    Date tmpDate = new Date();
	    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z Z)");

	    tz = TimeZone.getTimeZone("GMT+7:00");
	    df.setTimeZone(tz);
	    
	    date = df.parse(df.format(tmpDate));
	    
	    System.err.println("#@#!#!#@!#!!!!!!@@! " + date);
		 
	}
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@SuppressWarnings("rawtypes")
	public void prepare() throws Exception {
		this.setHeader();
		this.makeKoDate();

		try{
			params = new HashMap();
			bind(request, params);
		}catch(Exception e){
			e.printStackTrace();
		}
		this.makeLoginSession();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void bind(HttpServletRequest request, Map command) throws Exception {
		Enumeration parameters = request.getParameterNames();
		String parameterName = "";
		String[] values = null; 
		
		while (parameters.hasMoreElements()) {
			parameterName = parameters.nextElement().toString();
			values = request.getParameterValues(parameterName);
			if (values.length == 1) {
				command.put(parameterName, values[0]);
			}
		}
	}
	
	public Object getModel() {
		return new Object();
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	
	/**
	 * @brief 
	 * @return the siteConstService
	 */
	public SiteConstService getSiteConstService() {
		return siteConstService;
	}

	/**
	 * @brief 
	 * @param siteConstService the siteConstService to set
	 */
	public void setSiteConstService(SiteConstService siteConstService) {
		this.siteConstService = siteConstService;
	}

	
	/**
	 * @brief 
	 * @return the userBean
	 */
	public UserBean getUserBean() {
		return userBean;
	}

	/**
	 * @brief 
	 * @param userBean the userBean to set
	 */
	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
}
